package com.adlotto2.android.cronos.c2dm;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.NotificationManager;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.provider.Settings;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.adlotto2.android.LottoAdApplication;
import com.adlotto2.android.common.LottoAdFullActivity;

/**
 * Push Message Activity
 * @author Administrator
 *
 */
public class ShowMsg extends Activity {

	private String mTitle, mMsg, mSendSeq, mType;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		
		LinearLayout background = new LinearLayout(this);
		background.setBackgroundColor(Color.BLACK);
		setContentView(background);
		
		NotificationManager notificationManager = (NotificationManager) getSystemService(Activity.NOTIFICATION_SERVICE);

		getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED );  

		Intent intent = getIntent();
		
		mSendSeq = intent.getStringExtra("sendSeq");
		mType = intent.getStringExtra("type");

		Bundle bun = getIntent().getExtras();

		mTitle = bun.getString("title");
		mMsg = bun.getString("msg");
		
		
		
		
		
		
    	/*Intent mainIntent = new Intent(this, LottoAdMainActivity.class);
		mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK 
                | Intent.FLAG_ACTIVITY_CLEAR_TOP 
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    	
    	PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, mainIntent, 0);
    	
    	 Notification noti = new Notification.Builder(this)
         .setContentTitle(mTitle)
         .setContentText(mMsg)
         .setSmallIcon(R.drawable.icon1_2)
         .setVibrate(new long[] { 500, 100, 500, 100 })
         .getNotification();
    	 notificationManager.notify(0, noti);*/
		
		
		
		
		
		
		
		
		
		AlertDialog.Builder alertDialog = new AlertDialog.Builder(ShowMsg.this);
		
		if(mType.equals("0")){
			alertDialog.setPositiveButton("확인",
					new DialogInterface.OnClickListener() {
	
						@Override
						public void onClick(DialogInterface dialog, int which) {
							
							NotificationManager notiManager = (NotificationManager) getSystemService(Activity.NOTIFICATION_SERVICE);
							notiManager.cancel(0);
							
							PushWakeLock.releaseCpuLock();
							
							
							Intent intent = new Intent(ShowMsg.this, LottoAdFullActivity.class);
							intent.putExtra("sendSeq", mSendSeq);
							intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK 
					                | Intent.FLAG_ACTIVITY_CLEAR_TOP 
					                | Intent.FLAG_ACTIVITY_SINGLE_TOP);
							startActivity(intent);
							ShowMsg.this.finish();
						}
	
					});
	
			alertDialog.setNegativeButton("취소",
					new DialogInterface.OnClickListener() {
	
						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
							finish();
							PushWakeLock.releaseCpuLock();
							/*Intent intent = new Intent(ShowMsg.this, LottoAdMainActivity.class);
							intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK 
					                | Intent.FLAG_ACTIVITY_CLEAR_TOP 
					                | Intent.FLAG_ACTIVITY_SINGLE_TOP);
							startActivity(intent);
							ShowMsg.this.finish();*/
						}
	
					});
		} else {
			alertDialog.setPositiveButton("확인",
					new DialogInterface.OnClickListener() {
	
						@Override
						public void onClick(DialogInterface dialog, int which) {
							ShowMsg.this.finish();
						}
	
					});
		}

		alertDialog.setTitle(mTitle);
		alertDialog.setMessage(mMsg);
		alertDialog.show();


		int defTimeOut = Settings.System.getInt(getContentResolver(),
				Settings.System.SCREEN_OFF_TIMEOUT, 15000);

		TimerTask task = new TimerTask() {

			@Override
			public void run() {

				PushWakeLock.releaseCpuLock();

			}

		};

		Timer timer = new Timer();

		timer.schedule(task, defTimeOut);

	}
	
	public void goBack() {
		super.onBackPressed();
	}

}
